from typing import Optional

from sqlalchemy import BigInteger, Computed, DECIMAL, Date, DateTime, Index, Integer, String, text
from sqlalchemy.dialects.mysql import DECIMAL, MEDIUMTEXT, TINYINT, VARCHAR
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
import datetime
import decimal

class Base(DeclarativeBase):
    pass


class OpOrder(Base):
    __tablename__ = 'op_order'
    __table_args__ = (
        Index('idx_confirm_time', 'confirm_time'),
        Index('idx_confirm_time_after_risk', 'confirm_time', 'after_sales_status', 'risk_control_status'),
        Index('idx_created_time', 'created_time'),
        Index('idx_pay_time', 'pay_time'),
        Index('op_create_idx', 'create_time'),
        Index('op_shop_created_idx', 'shop_id', 'created_time'),
        Index('op_sp_order_idx', 'order_sn'),
        Index('op_sp_status_created_idx', 'after_sales_status', 'risk_control_status', 'created_time'),
        {'comment': '拼多多订单表'}
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, comment='主键')
    bidding_pirce: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(10, 2), comment='竞价价格')
    order_sn: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='订单编码')
    shop_name: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='店铺名')
    shop_id: Mapped[Optional[int]] = mapped_column(Integer, comment='店铺id')
    buyer_memo: Mapped[Optional[str]] = mapped_column(VARCHAR(512), comment='卖家留言信息')
    city: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='收件城市')
    city_id: Mapped[Optional[int]] = mapped_column(Integer, comment='收件城市编码')
    confirm_status: Mapped[Optional[int]] = mapped_column(Integer, comment='成交状态：0：未成交、1：已成交、2：已取消')
    confirm_time: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='成交时间')
    created_time: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='创建时间')
    goods_amount: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(10, 2), comment='商品金额（元），商品金额=商品销售价格*商品数量-订单改价折扣金额')
    pay_amount: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(10, 2), comment='支付金额（元），支付金额=商品金额-折扣金额+邮费+服务费')
    pay_no: Mapped[Optional[str]] = mapped_column(VARCHAR(150), comment='支付单号')
    pay_time: Mapped[Optional[str]] = mapped_column(VARCHAR(150), comment='支付时间')
    trade_type: Mapped[Optional[int]] = mapped_column(Integer, comment='订单类型：0-普通订单、1-定金订单')
    shipping_time: Mapped[Optional[str]] = mapped_column(VARCHAR(150), comment='发货时间')
    logistics_id: Mapped[Optional[str]] = mapped_column(MEDIUMTEXT, comment='快递公司编号')
    order_status: Mapped[Optional[int]] = mapped_column(Integer, comment='发货状态，枚举值：1：待发货，2：已发货待签收，3：已签收')
    create_by: Mapped[Optional[int]] = mapped_column(BigInteger, comment='创建人')
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='创建时间')
    update_by: Mapped[Optional[int]] = mapped_column(BigInteger, comment='修改人')
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='修改时间')
    deleted: Mapped[Optional[int]] = mapped_column(TINYINT(1), server_default=text("'0'"), comment='是否删除')
    tracking_number: Mapped[Optional[str]] = mapped_column(VARCHAR(100), comment='运单号')
    after_sales_status: Mapped[Optional[int]] = mapped_column(Integer, comment='售后状态 0：无售后 2：买家申请退款，待商家处理 3：退货退款，待商家处理 4：商家同意退款，退款中 5：平台同意退款，退款中 6：驳回退款，待买家处理 7：已同意退货退款,待用户发货 8：平台处理中 9：平台拒绝退款，退款关闭 10：退款成功 11：买家撤销 12：买家逾期未处理，退款失败 13：买家逾期，超过有效期 14：换货补寄待商家处理 15：换货补寄待用户处理 16：换货补寄成功 17：换货补寄失败 18：换货补寄待用户确认完成 21：待商家同意维修 22：待用户确认发货 24：维修关闭 25：维修成功 27：待用户确认收货 31：已同意拒收退款，待用户拒收 32：补寄待商家发货')
    cat_id_1: Mapped[Optional[int]] = mapped_column(Integer, comment='商品一级分类')
    cat_id_2: Mapped[Optional[int]] = mapped_column(Integer, comment='商品二级分类')
    cat_id_3: Mapped[Optional[int]] = mapped_column(Integer, comment='商品三级分类')
    cat_id_4: Mapped[Optional[int]] = mapped_column(Integer, comment='商品四级分类')
    free_sf: Mapped[Optional[int]] = mapped_column(Integer, comment='是否顺丰包邮，1-是 0-否')
    updated_at: Mapped[Optional[str]] = mapped_column(VARCHAR(100), comment='订单最近一次更新时间')
    town_id: Mapped[Optional[int]] = mapped_column(Integer, comment='区县编码')
    town: Mapped[Optional[str]] = mapped_column(VARCHAR(256), comment='收件地区县')
    support_nationwide_warranty: Mapped[Optional[int]] = mapped_column(Integer, comment='全国联保，1:是，0:否')
    stock_out_handle_status: Mapped[Optional[int]] = mapped_column(Integer, comment='缺货处理状态，-1:无缺货处理 0: 缺货待处理 1缺货已处理')
    step_trade_status: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(10, 2), comment='定金订单状态：0-定金未付尾款未付、1-定金已付尾款未付、2-定金已付尾款已付')
    step_discount_amount: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(10, 2), comment='膨胀金额 单位：元')
    ship_additional_origin_order: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='加运费补差价订单的原单')
    ship_additional_link_order: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='关联的加运费发顺丰的补差价订单')
    seller_discount: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(10, 2), comment='店铺优惠金额')
    self_contained: Mapped[Optional[int]] = mapped_column(Integer, comment='是否门店自提，1-是，0-否')
    risk_control_status: Mapped[Optional[int]] = mapped_column(Integer, comment='订单审核状态（0-正常订单， 1-审核中订单）')
    return_freight_payer: Mapped[Optional[int]] = mapped_column(Integer, comment='退货包运费，1:是，0:否')
    refund_status: Mapped[Optional[int]] = mapped_column(Integer, comment='退款状态，枚举值：1：无售后或售后关闭，2：售后处理中，3：退款中，4： 退款成功')
    group_status: Mapped[Optional[int]] = mapped_column(Integer, comment='成团状态：0：拼团中、1：已成团、2：团失败')
    last_ship_time: Mapped[Optional[str]] = mapped_column(VARCHAR(150), comment='最晚发货时间')
    platform_discount: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(14, 4), comment='平台优惠金额')
    national_subsidy: Mapped[Optional[int]] = mapped_column(Integer, server_default=text("'0'"), comment='是否国补 0 非国补 1 国补')
    activity_type: Mapped[Optional[str]] = mapped_column(String(10, 'utf8mb4_general_ci'), server_default=text("'0'"), comment='活动类型  百亿补贴 205 秒杀补贴212 新百亿分省215 新秒杀分省216 国补百亿2150 国补秒杀2160 百亿分省211 秒杀分省213 多人团14')


class OpOrderGoods(Base):
    __tablename__ = 'op_order_goods'
    __table_args__ = (
        Index('idx_create_time', 'create_time'),
        Index('idx_goods_id', 'goods_id'),
        Index('idx_op_order_id', 'op_order_id'),
        Index('idx_order_created_time', 'order_created_time', 'outer_id'),
        Index('idx_order_created_timestamp', 'order_created_timestamp', 'order_created_time', 'outer_id'),
        Index('idx_sku_id', 'sku_id'),
        Index('op_order_idx', 'op_order_id', 'goods_id', 'outer_id'),
        Index('op_outer_idx', 'outer_id', 'order_created_time'),
        {'comment': '拼多多订单表商品表'}
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, comment='主键')
    order_sn: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='订单编码')
    op_order_id: Mapped[Optional[int]] = mapped_column(BigInteger, comment='op_sp_order主键')
    goods_count: Mapped[Optional[int]] = mapped_column(Integer, comment='商品数量')
    goods_id: Mapped[Optional[str]] = mapped_column(VARCHAR(100), comment='商品编码')
    goods_img: Mapped[Optional[str]] = mapped_column(VARCHAR(512), comment='商品图片')
    goods_name: Mapped[Optional[str]] = mapped_column(VARCHAR(512), comment='商品名称')
    goods_price: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(10, 2), comment='商品单件 单价：元')
    goods_spec: Mapped[Optional[str]] = mapped_column(VARCHAR(200), comment='商品规格')
    outer_goods_id: Mapped[Optional[str]] = mapped_column(VARCHAR(200), comment='商品维度外部编码，注意：编辑商品后必须等待商品审核通过后方可生效，订单中商品信息为交易快照的商品信息')
    outer_id: Mapped[Optional[str]] = mapped_column(VARCHAR(200), comment='sku维度商家外部编码，注意：编辑商品后必须等待商品审核通过后方可生效，订单中商品信息为交易快照的商品信息。')
    sku_id: Mapped[Optional[str]] = mapped_column(VARCHAR(200), comment='商品sku编码')
    create_by: Mapped[Optional[int]] = mapped_column(BigInteger, comment='创建人')
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='创建时间')
    update_by: Mapped[Optional[int]] = mapped_column(BigInteger, comment='修改人')
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='修改时间')
    deleted: Mapped[Optional[int]] = mapped_column(TINYINT(1), server_default=text("'0'"), comment='是否删除')
    order_created_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime)
    activity_type: Mapped[Optional[str]] = mapped_column(VARCHAR(5), server_default=text("''"), comment='活动类型')
    order_created_timestamp: Mapped[Optional[str]] = mapped_column(VARCHAR(8), Computed("(date_format(`order_created_time`,_utf8mb4'%T'))", persisted=False), comment='虚拟列，用于函数查询')


class OpOrderGoodsTb(Base):
    __tablename__ = 'op_order_goods_tb'
    __table_args__ = (
        Index('idx_created_time_outer_id', 'order_created_time', 'outer_sku_id', 'op_order_tb_id'),
        Index('idx_created_timestamp', 'idx_created_timestamp'),
        Index('idx_num_iid', 'num_iid'),
        Index('idx_op_order_tb_id', 'op_order_tb_id'),
        Index('idx_outer_iid', 'outer_iid'),
        Index('idx_outer_sku_id', 'outer_sku_id', 'order_created_time'),
        Index('idx_sku_id', 'sku_id'),
        Index('idx_tid', 'tid'),
        {'comment': '淘宝订单表商品表'}
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, comment='主键')
    tid: Mapped[int] = mapped_column(BigInteger, comment='交易编号 (父订单的交易编号)')
    op_order_tb_id: Mapped[Optional[int]] = mapped_column(BigInteger, comment='订单主键')
    item_meal_name: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='套餐的值。如：M8原装电池:便携支架:M8专用座充:莫凡保护袋')
    pic_path: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='商品图片的绝对路径')
    refund_status: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='退款状态。退款状态。可选值 WAIT_SELLER_AGREE(买家已经申请退款，等待卖家同意) WAIT_BUYER_RETURN_GOODS(卖家已经同意退款，等待买家退货) WAIT_SELLER_CONFIRM_GOODS(买家已经退货，等待卖家确认收货) SELLER_REFUSE_BUYER(卖家拒绝退款) CLOSED(退款关闭) SUCCESS(退款成功)')
    outer_iid: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='商家外部编码(可与商家外部系统对接)。外部商家自己定义的商品Item的id，可以通过taobao.items.custom.get获取商品的Item的信息')
    buyer_rate: Mapped[Optional[int]] = mapped_column(TINYINT, comment='买家是否已评价。可选值：true(已评价)，false(未评价)')
    seller_rate: Mapped[Optional[int]] = mapped_column(TINYINT, comment='卖家是否已评价。可选值：true(已评价)，false(未评价)')
    seller_type: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='卖家类型，可选值为：B（商城商家），C（普通卖家）')
    cid: Mapped[Optional[int]] = mapped_column(Integer, comment='交易商品对应的类目ID')
    sub_order_tax_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='天猫国际官网直供子订单关税税费')
    sub_order_tax_rate: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='天猫国际官网直供子订单关税税率')
    oid: Mapped[Optional[int]] = mapped_column(BigInteger, comment='子订单编号')
    status: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='订单状态（请关注此状态，如果为TRADE_CLOSED_BY_TAOBAO状态，则不要对此订单进行发货，切记啊！）。可选值:\r\nTRADE_NO_CREATE_PAY(没有创建支付宝交易)\r\nWAIT_BUYER_PAY(等待买家付款)\r\nWAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)\r\nWAIT_BUYER_CONFIRM_GOODS(等待买家确认收货,即:卖家已发货)\r\nTRADE_BUYER_SIGNED(买家已签收,货到付款专用)\r\nTRADE_FINISHED(交易成功)\r\nTRADE_CLOSED(付款以后用户退款成功，交易自动关闭)\r\nTRADE_CLOSED_BY_TAOBAO(付款以前，卖家或买家主动关闭交易)\r\nPAY_PENDING(国际信用卡支付付款确认中)')
    title: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='商品标题')
    price: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='商品价格。精确到2位小数;单位:元。如:200.07，表示:200元7分')
    num_iid: Mapped[Optional[int]] = mapped_column(BigInteger, comment='商品数字ID')
    item_meal_id: Mapped[Optional[int]] = mapped_column(BigInteger, comment='套餐ID')
    sku_id: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='商品的最小库存单位Sku的id.可以通过taobao.item.sku.get获取详细的Sku信息')
    num: Mapped[Optional[int]] = mapped_column(BigInteger, comment='买数量。取值范围:大于零的整数')
    outer_sku_id: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='外部网店自己定义的Sku编号')
    total_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='应付金额（商品价格 * 商品数量 + 手工调整金额 - 子订单级订单优惠金额）。精确到2位小数;单位:元。如:200.07，表示:200元7分')
    payment: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='\t子订单实付金额。精确到2位小数，单位:元。如:200.07，表示:200元7分。对于多子订单的交易，计算公式如下：payment = price * num + adjust_fee - discount_fee ；单子订单交易，payment与主订单的payment一致，对于退款成功的子订单，由于主订单的优惠分摊金额，会造成该字段可能不为0.00元。建议使用退款前的实付金额减去退款单中的实际退款金额计算。')
    discount_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='子订单级订单优惠金额。精确到2位小数;单位:元。如:200.07，表示:200元7分')
    adjust_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='手工调整金额.格式为:1.01;单位:元;精确到小数点后两位.')
    sku_properties_name: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='SKU的值。如：机身颜色:黑色;手机套餐:官方标配')
    refund_id: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='最近退款ID')
    is_service_order: Mapped[Optional[int]] = mapped_column(TINYINT, comment='是否是服务订单，是返回true，否返回false。')
    end_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='子订单的交易结束时间说明：子订单有单独的结束时间，与主订单的结束时间可能有所不同，在有退款发起的时候或者是主订单分阶段付款的时候，子订单的结束时间会早于主订单的结束时间，所以开放这个字段便于订单结束状态的判断')
    consign_time: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='子订单发货时间，当卖家对订单进行了多次发货，子订单的发货时间和主订单的发货时间可能不一样了，那么就需要以子订单的时间为准。（没有进行多次发货的订单，主订单的发货时间和子订单的发货时间都一样）')
    shipping_type: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='子订单的运送方式（卖家对订单进行多次发货之后，一个主订单下的子订单的运送方式可能不同，用order.shipping_type来区分子订单的运送方式）')
    bind_oid: Mapped[Optional[int]] = mapped_column(BigInteger, comment='捆绑的子订单号，表示该子订单要和捆绑的子订单一起发货，用于卖家子订单捆绑发货')
    logistics_company: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='子订单发货的快递公司名称')
    invoice_no: Mapped[Optional[str]] = mapped_column(VARCHAR(100), comment='子订单所在包裹的运单号')
    is_daixiao: Mapped[Optional[int]] = mapped_column(TINYINT, comment='表示订单交易是否含有对应的代销采购单。如果该订单中存在一个对应的代销采购单，那么该值为true；反之，该值为false。')
    divide_order_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='分摊之后的实付金额')
    part_mjz_discount: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='优惠分摊')
    ticket_outer_id: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='对应门票有效期的外部id')
    ticket_expdate_key: Mapped[Optional[str]] = mapped_column(VARCHAR(2000), comment='门票有效期的key')
    store_code: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='发货的仓库编码')
    is_www: Mapped[Optional[int]] = mapped_column(TINYINT, comment='子订单是否是www订单')
    tmser_spu_code: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='支持家装类物流的类型')
    zhengji_status: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='征集预售订单征集状态：1（征集中），2（征集成功），3（征集失败）')
    md_qualification: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='免单资格属性')
    md_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='免单金额')
    sub_order_tax_promotion_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='天猫国际子订单计税优惠金额')
    tax_coupon_discount: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='天猫国际订单包税金额')
    tax_free: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='天猫国际订单是否包税')
    special_refund_type: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='特殊的退款类型，比如直播返现、价保')
    is_devalue_fee: Mapped[Optional[int]] = mapped_column(TINYINT, comment='\t子订单优惠贬值')
    is_idle: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='是否为闲鱼订单，1是，0否')
    create_by: Mapped[Optional[int]] = mapped_column(BigInteger, comment='创建人')
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='创建时间')
    update_by: Mapped[Optional[int]] = mapped_column(BigInteger, comment='修改人')
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='修改时间')
    deleted: Mapped[Optional[int]] = mapped_column(TINYINT(1), server_default=text("'0'"), comment='是否删除')
    order_created_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='订单创建时间，（冗余列）')
    idx_created_timestamp: Mapped[Optional[str]] = mapped_column(VARCHAR(8), Computed("(date_format(`order_created_time`,_utf8mb4'%T'))", persisted=False), comment='虚拟列，用于函数查询')


class OpOrderTb(Base):
    __tablename__ = 'op_order_tb'
    __table_args__ = (
        Index('idx_create_time', 'create_time'),
        Index('idx_created', 'created', 'pay_time'),
        Index('idx_num_iid', 'num_iid'),
        Index('idx_op_order_tb_modified', 'modified'),
        Index('idx_shop_id', 'shop_id'),
        Index('idx_tid', 'tid'),
        {'comment': '淘宝订单表'}
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, comment='主键')
    shop_id: Mapped[Optional[int]] = mapped_column(BigInteger, comment='店铺ID(op_pdd_config表shop_id)')
    shop_name: Mapped[Optional[str]] = mapped_column(VARCHAR(100), comment='店铺名称(op_pdd_config表shop_name)')
    seller_nick: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='卖家昵称')
    pic_path: Mapped[Optional[str]] = mapped_column(VARCHAR(2000), comment='商品图片绝对途径')
    payment: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='实付金额。精确到2位小数;单位:元。如:200.07，表示:200元7分')
    seller_rate: Mapped[Optional[int]] = mapped_column(TINYINT, comment='卖家是否已评价。可选值:true(已评价),false(未评价)')
    post_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='邮费。精确到2位小数;单位:元。如:200.07，表示:200元7分')
    receiver_name: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='收货人的姓名')
    receiver_state: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='收货人的所在省份')
    receiver_address: Mapped[Optional[str]] = mapped_column(VARCHAR(3000), comment='收货人的详细地址')
    receiver_zip: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='收货人的邮编')
    receiver_mobile: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='收货人的手机号码')
    receiver_phone: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='收货人的电话号码')
    consign_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='卖家发货时间。格式:yyyy-MM-dd HH:mm:ss')
    received_payment: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='卖家实际收到的支付宝打款金额（由于子订单可以部分确认收货，这个金额会随着子订单的确认收货而不断增加，交易成功后等于买家实付款减去退款金额）。精确到2位小数;单位:元。如:200.07，表示:200元7分')
    receiver_country: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='收货人国籍')
    receiver_town: Mapped[Optional[str]] = mapped_column(VARCHAR(3000), comment='收货人街道地址')
    order_tax_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='天猫国际官网直供主订单关税税费')
    tid: Mapped[Optional[int]] = mapped_column(BigInteger, comment='交易编号 (父订单的交易编号)')
    num: Mapped[Optional[int]] = mapped_column(BigInteger, comment='\t商品购买数量。取值范围：大于零的整数,对于一个trade对应多个order的时候（一笔主订单，对应多笔子订单），num=0，num是一个跟商品关联的属性，一笔订单对应多比子订单的时候，主订单上的num无意义。')
    num_iid: Mapped[Optional[int]] = mapped_column(BigInteger, comment='商品数字编号')
    status: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='交易状态。可选值: * TRADE_NO_CREATE_PAY(没有创建支付宝交易) * WAIT_BUYER_PAY(等待买家付款) * SELLER_CONSIGNED_PART(卖家部分发货) * WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款) * WAIT_BUYER_CONFIRM_GOODS(等待买家确认收货,即:卖家已发货) * TRADE_BUYER_SIGNED(买家已签收,货到付款专用) * TRADE_FINISHED(交易成功) * TRADE_CLOSED(付款以后用户退款成功，交易自动关闭) * TRADE_CLOSED_BY_TAOBAO(付款以前，卖家或买家主动关闭交易) * PAY_PENDING(国际信用卡支付付款确认中) * WAIT_PRE_AUTH_CONFIRM(0元购合约中) * PAID_FORBID_CONSIGN(拼团中订单或者发货强管控的订单，已付款但禁止发货)')
    title: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='交易标题，以店铺名作为此标题的值。注:taobao.trades.get接口返回的Trade中的title是商品名称')
    type: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='交易类型列表，同时查询多种交易类型可用逗号分隔。默认同时查询guarantee_trade, auto_delivery, ec, cod的4种交易类型的数据 可选值 fixed(一口价) auction(拍卖) guarantee_trade(一口价、拍卖) auto_delivery(自动发货) independent_simple_trade(旺店入门版交易) independent_shop_trade(旺店标准版交易) ec(直冲) cod(货到付款) fenxiao(分销) game_equipment(游戏装备) shopex_trade(ShopEX交易) netcn_trade(万网交易) external_trade(统一外部交易)o2o_offlinetrade（O2O交易）step (万人团)nopaid(无付款订单)pre_auth_type(预授权0元购机交易)')
    price: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='商品价格。精确到2位小数；单位：元。如：200.07，表示：200元7分')
    discount_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='可以使用trade.promotion_details查询系统优惠系统优惠金额（如打折，VIP，满就送等），精确到2位小数，单位：元。如：200.07，表示：200元7分')
    total_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='商品金额（商品价格乘以数量的总金额）。精确到2位小数;单位:元。如:200.07，表示:200元7分')
    created: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='交易创建时间。格式:yyyy-MM-dd HH:mm:ss')
    pay_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='付款时间。格式:yyyy-MM-dd HH:mm:ss。订单的付款时间即为物流订单的创建时间。')
    modified: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='交易修改时间(用户对订单的任何修改都会更新此字段)。格式:yyyy-MM-dd HH:mm:ss')
    end_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='交易结束时间。交易成功时间(更新交易状态为成功的同时更新)/确认收货时间或者交易关闭时间 。格式:yyyy-MM-dd HH:mm:ss')
    seller_flag: Mapped[Optional[int]] = mapped_column(Integer, comment='卖家备注旗帜（与淘宝网上订单的卖家备注旗帜对应，只有卖家才能查看该字段）红、黄、绿、蓝、紫 分别对应 1、2、3、4、5')
    buyer_nick: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='买家昵称')
    has_buyer_message: Mapped[Optional[int]] = mapped_column(TINYINT, comment='判断订单是否有买家留言，有买家留言返回true，否则返回false')
    credit_card_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='使用信用卡支付金额数')
    step_trade_status: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='分阶段付款的订单状态（例如万人团订单等），目前有三返回状态FRONT_NOPAID_FINAL_NOPAID(定金未付尾款未付)，FRONT_PAID_FINAL_NOPAID(定金已付尾款未付)，FRONT_PAID_FINAL_PAID(定金和尾款都付)')
    step_paid_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='分阶段付款的已付金额（万人团订单已付金额）')
    mark_desc: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='订单出现异常问题的时候，给予用户的描述,没有异常的时候，此值为空')
    buyer_open_uid: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='\t买家OpenUid')
    shipping_type: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='创建交易时的物流方式（交易完成前，物流方式有可能改变，但系统里的这个字段一直不变）。可选值：free(卖家包邮),post(平邮),express(快递),ems(EMS),virtual(虚拟发货)，25(次日必达)，26(预约配送)。')
    adjust_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='卖家手工调整金额，精确到2位小数，单位：元。如：200.07，表示：200元7分。来源于订单价格修改，如果有多笔子订单的时候，这个为0，单笔的话则跟[order].adjust_fee一样')
    buyer_rate: Mapped[Optional[int]] = mapped_column(TINYINT, comment='买家是否已评价。可选值:true(已评价),false(未评价)。如买家只评价未打分，此字段仍返回false')
    receiver_city: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='收货人的所在城市\r\n注：因为国家对于城市和地区的划分的有：省直辖市和省直辖县级行政区（区级别的）划分的，淘宝这边根据这个差异保存在不同字段里面比如：广东广州：广州属于一个直辖市是放在的receiver_city的字段里面；而河南济源：济源属于省直辖县级行政区划分，是区级别的，放在了receiver_district里面\r\n建议：程序依赖于城市字段做物流等判断的操作，最好加一个判断逻辑：如果返回值里面只有receiver_district参数，该参数作为城市')
    receiver_district: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='收货人的所在地区\r\n注：因为国家对于城市和地区的划分的有：省直辖市和省直辖县级行政区（区级别的）划分的，淘宝这边根据这个差异保存在不同字段里面比如：广东广州：广州属于一个直辖市是放在的receiver_city的字段里面；而河南济源：济源属于省直辖县级行政区划分，是区级别的，放在了receiver_district里面\r\n建议：程序依赖于城市字段做物流等判断的操作，最好加一个判断逻辑：如果返回值里面只有receiver_district参数，该参数作为城市')
    shop_pick: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='门店自提，总店发货，分店取货的门店自提订单标识')
    rx_audit_status: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='处方药未审核状态')
    post_gate_declare: Mapped[Optional[int]] = mapped_column(TINYINT, comment='邮关订单')
    cross_bonded_declare: Mapped[Optional[int]] = mapped_column(TINYINT, comment='跨境订单')
    order_tax_promotion_fee: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='天猫国际计税优惠金额')
    service_type: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='serviceType')
    is_o2o_passport: Mapped[Optional[int]] = mapped_column(TINYINT, comment='是否是智慧门店订单，只有true，或者 null 两种情况')
    oaid: Mapped[Optional[str]] = mapped_column(VARCHAR(300), comment='（收货人+手机号+座机+收货地址+create）5个字段组合成oaid，原始订单上座机为空也满足条件，否则生成不了oaid')
    no_shipping: Mapped[Optional[int]] = mapped_column(TINYINT, comment='无物流信息返回true，平台属性，业务不要依赖')
    aid: Mapped[Optional[str]] = mapped_column(VARCHAR(300), comment='地址aid字段')
    create_by: Mapped[Optional[int]] = mapped_column(BigInteger, comment='创建人')
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='创建时间')
    update_by: Mapped[Optional[int]] = mapped_column(BigInteger, comment='修改人')
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='修改时间')
    deleted: Mapped[Optional[int]] = mapped_column(TINYINT(1), server_default=text("'0'"), comment='是否删除')
    subsidy_discount_fee: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(16, 6), comment='百亿补贴优惠金额')
    real_pay_amount: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(16, 6), comment='订单实际支付金额（支付金额-百亿补贴优惠金额）')
    forecast_profit: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(16, 6), comment='预计利润')
    goods_cost_price: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(16, 6), comment='商品成本价')
    inner_cost_price: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(16, 6), comment='内部成本价')
    real_subsidy_amount: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(16, 6), comment='实际百补金额')
    national_subsidy: Mapped[Optional[int]] = mapped_column(Integer, server_default=text("'0'"), comment='是否国补 0 非国补 1 国补')
    activity_type: Mapped[Optional[str]] = mapped_column(String(10, 'utf8mb4_general_ci'), server_default=text("'0'"), comment='活动类型 日常团4 多人团5 限时秒3 超链半托管8 国补超链半托管9')


class OpOrderedBiddingPrice(Base):
    __tablename__ = 'op_ordered_bidding_price'
    __table_args__ = (
        Index('idx_create_time', 'create_time'),
        Index('idx_orderMarketOcc', 'outer_id', 'bidding_date', 'sku_id', 'plat_form_type'),
        Index('idx_ordered_bidding_price_gc', 'goods_name', 'plat_form_type', 'create_time'),
        Index('idx_shop_name', 'shop_name'),
        Index('idx_shop_name_create_goods', 'shop_name', 'create_time', 'goods_id'),
        Index('op_sku_shop_create', 'sku_id', 'shop_name', 'create_time'),
        {'comment': '顺位竞价信息表（只包含百亿补贴活动）'}
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, comment='主键')
    shop_name: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='店铺名字')
    goods_id: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='商品id')
    sku_id: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='拼多多skuId')
    goods_name: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='拼多多商品名字')
    spec: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='规格spec')
    sku_name: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='skuName')
    bid_price: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(10, 2), comment='价格(单位 分)')
    bid_order: Mapped[Optional[str]] = mapped_column(VARCHAR(8), comment='顺位')
    start_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='竞价开始时间')
    end_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='竞价结束时间')
    publish_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='中标时间')
    publish_time_end: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='结束时间')
    refuse_reason: Mapped[Optional[str]] = mapped_column(VARCHAR(255), comment='中标原因')
    highest_apply_price: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(16, 6), comment='建议价')
    supply_price: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(16, 6), comment='淘宝最大供应价格')
    bid_max_price: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(16, 6), comment='淘宝最大供应价格')
    create_by: Mapped[Optional[int]] = mapped_column(BigInteger, comment='创建人')
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='创建时间')
    update_by: Mapped[Optional[int]] = mapped_column(BigInteger, comment='修改人')
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='修改时间')
    deleted: Mapped[Optional[int]] = mapped_column(TINYINT(1), server_default=text("'0'"), comment='是否删除')
    activity_inventory: Mapped[Optional[int]] = mapped_column(BigInteger)
    outer_id: Mapped[Optional[str]] = mapped_column(VARCHAR(50), comment='规格编码')
    bidding_date: Mapped[Optional[datetime.date]] = mapped_column(Date, server_default=text('(curdate())'), comment='竞价日期')
    plat_form_type: Mapped[Optional[int]] = mapped_column(Integer, comment='平台类型')
    crawler_type: Mapped[Optional[int]] = mapped_column(TINYINT, server_default=text("'0'"), comment='爬虫类型')
    cut_price_limit: Mapped[Optional[decimal.Decimal]] = mapped_column(DECIMAL(16, 6), comment='降价幅度')
    cut_price_times_remained: Mapped[Optional[int]] = mapped_column(Integer, comment='降价剩余次数')
    rise_price_times_remained: Mapped[Optional[int]] = mapped_column(Integer, comment='涨价剩余次数')
